<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="ass_img">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单区域 -->
      <!-- 绑定表单数据 -->
      <el-form label-width="0px" ref="loginFormRef11" :model="Form" :rules="loginForm" class="logo_form">
          <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input  v-model="Form.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <!-- 密码 -->
          <el-form-item prop="password">
              <!-- 设置密码属性 -->
          <el-input type="password" v-model="Form.password"  prefix-icon="el-icon-s-goods"></el-input>
        </el-form-item>
        <!-- 按钮 -->
         <el-form-item  class="button_right">
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info" @click="resetdelete">重置</el-button>
         </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
    data(){
        return {
            // 表单数据绑定
            Form:{
                username:'12',
                password:'23'
            },
            // 这是表单验证规则
            loginForm:{
                //验证用户名是否合法
                username:[
                    // required表示必填项，message表示输入错误的时候报错信息，trigger表示失去焦点触发事件
                    { required: true, message: '请输入正确的用户名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                // 验证密码
                password:[
                     { required: true, message: '请输入正确密码', trigger: 'blur' },
                     { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
        // 重置事件
        resetdelete(){
           console.log(this)
        // 重置错误提示
            this.$refs.loginFormRef11.resetFields();
        },
        // 处理登录的之前的预验证  validate是element ui 里面的表单验证
        login(){
            this.$refs.loginFormRef11.validate(async valid=>{
                if(!valid) return;
                const {data:res}=await this.$http.post("/yonghu",this.Form)
                
                 console.log(res)
                  window.sessionStorage.setItem("token",res.username)
                  this.$router.push("/home");
                  this.$message.success("登录成功") 
                // 将登录成功之后的token，保存客户端的sessionSrorage中
                
                
                // 2.通过编程式导航跳转后台主页，路由地址是/home
               
            })
        }
    }
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: white;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.ass_img {
  width: 130px;
  height: 130px;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.button_right{
    display: flex;
    justify-content:flex-end;
}
.logo_form{
    position:absolute;
    bottom: 0;
    width:100%;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>